package app;

import android.app.Application;
import android.content.Context;
import android.os.Environment;
import android.support.multidex.MultiDex;
import com.nostra13.universalimageloader.cache.disc.impl.UnlimitedDiskCache;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.uzmap.pkg.openapi.APICloud;
import org.apache.log4j.Level;
import org.xutils.x;
import java.io.File;
import cn.jpush.android.api.JPushInterface;
import data.SharedPreferencesUtils;
import de.mindpipe.android.logging.log4j.LogConfigurator;
import util.CrashHandler;
import util.HomeUtil;
import util.LogUtil;

/**
 * Created liuxiaobin15/11/2
 */
public class MyApplication extends Application {
    private static MyApplication mApp;
    private ImageLoader mImageLoader = null;
    private  Context mContext;
    private LogConfigurator logConfigurator;
    private static int mSiteAreaid;                  //后台返回的区域ID 1深大 2 深职院 3是4G 4商铺
    private static String mFWQString = "http://123.207.110.98:8080/wifi/TransferServlet";  //域名http://api.szaltair.com:8080 远程服务器
    public static final String mFWQIP = "http://123.207.110.98:8080/wifi/TransferServlet"; //对应的ip
    private static int mPattern = 0;  //后台返回的模式

    @Override
    public void onCreate() {
        super.onCreate();
        mApp = this;
        mContext = this;
        initHttpUtils();
        initUniversalImageLoader();
        LogUtil.eE("",".....MyApplication()..");
        Loginit();
        initData();
        JPushInterface.setDebugMode(true);           //正式版的时候设置false，关闭调试
        JPushInterface.init(this);

        try {
          APICloud.initialize(this);
        }catch (Exception e){
            LogUtil.eE("",".....MyApplication().."+e.getMessage());
        }finally {

        }
    }

    /**
     * 初始化HttpUtils
     */
    private void initHttpUtils() {
        x.Ext.init(this);
    }

    public static MyApplication getApp() {
        return mApp;
    }

    /**
     * 初始化UniversalImageLoader
     */
    public void initUniversalImageLoader() {
        mImageLoader = ImageLoader.getInstance();

        // 内存大小
        int cacheSize = 100 * 1024 * 1024;

        String cachePath = null;

        // 磁盘缓存路径
        if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())) {
            cachePath = getApplicationContext().getExternalCacheDir().getPath();
        } else {
            cachePath = getApplicationContext().getCacheDir().getPath();
        }

        File cacheFileDir = new File(cachePath, "/greentree/images");
        cacheFileDir.mkdirs();

        // UniversalImageLoader配置
        ImageLoaderConfiguration configuration =
                new ImageLoaderConfiguration
                        .Builder(getApplicationContext())
                        .memoryCacheSize(cacheSize)          // 内存缓存的最大值
                        .diskCache(new UnlimitedDiskCache(cacheFileDir))//自定义缓存路径
                        .diskCacheFileCount(300)          // 可以缓存的文件数量
                        .diskCacheSize(200 * 1024 * 1024)// 200 Mb sd卡(本地)缓存的最大值
                        .threadPoolSize(3)              // default  线程池内加载的数量
                        .build();

        // 初始化配置
        ImageLoader.getInstance().init(configuration);

    }
    public Context getAppContext() {
        return mContext;
    }
    public ImageLoader getImageLoader() {
        return mImageLoader;
    }

    public LogConfigurator getLogConfigurator() {
        return logConfigurator;
    }

    /**
     * 5.0 以下，单个DEX文件可被引用的方法总数被限制为65536 超过就会报错 NoClassDefFoundError
     * 这个方法是将一个DEX文件分拆成多个DEX文件
     */
    @Override
    protected void attachBaseContext(Context base) {
        super.attachBaseContext(base); MultiDex.install(this);
    }

    private void Loginit() {

        logConfigurator = new LogConfigurator();

        logConfigurator.setFileName(Environment.getExternalStorageDirectory()

                + File.separator + "NewFi" + File.separator + "logs"

                + File.separator + "log4j.txt");

        logConfigurator.setRootLevel(Level.ALL);
        logConfigurator.setLevel("org.apache", Level.ALL);

        logConfigurator.setFilePattern("%d %-5p [%c{2}]-[%L] %m%n");

        logConfigurator.setMaxFileSize(1024 * 1000 * 1);//每个文件200kb
        logConfigurator.setMaxBackupSize(50);
        try{
            logConfigurator.configure();
        }catch (Exception e){

        }finally {

        }
    }

    private void initData() {
        //当程序发生Uncaught异常的时候,由该类来接管程序,一定要在这里初始化
       CrashHandler.getInstance().init(this);
    }

    public String getFWQString() {
        return mFWQString;
    }

    public void setFWQString(String FWQString) {
        mFWQString = FWQString;
    }

    public int getSiteAreaid() {
        return mSiteAreaid;
    }

    public void setSiteAreaid(int siteAreaid) {
        mSiteAreaid = siteAreaid;
    }

    public int getmPattern() {
        mPattern = (int) SharedPreferencesUtils.getParam(mContext, HomeUtil.mAppPattern,2);
        return mPattern;
    }
}